package com.example.instantmessage;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.example.instantmessage.adapter.AppAdapter;
import com.example.instantmessage.fragment.ContactFragment;
import com.example.instantmessage.fragment.MessageFragment;
import com.example.instantmessage.fragment.MyselfFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.imsdk.v2.V2TIMSendCallback;
import com.tencent.imsdk.v2.V2TIMSimpleMsgListener;
import com.tencent.imsdk.v2.V2TIMUserInfo;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private BottomNavigationView bottomNavigationView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        viewPager = findViewById(R.id.home_viewpager);
        bottomNavigationView = findViewById(R.id.bottom_navigation_view);

        MessageFragment messageFragment = new MessageFragment();
        ContactFragment contactFragment = new ContactFragment();
        MyselfFragment myselfFragment = new MyselfFragment();

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(messageFragment);
        fragments.add(contactFragment);
        fragments.add(myselfFragment);

        AppAdapter appAdapter = new AppAdapter(getSupportFragmentManager(),0,fragments);
        viewPager.setAdapter(appAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        bottomNavigationView.setSelectedItemId(R.id.message);
                        break;
                    case 1:
                        bottomNavigationView.setSelectedItemId(R.id.contact);
                        break;
                    case 2:
                        bottomNavigationView.setSelectedItemId(R.id.me);
                        break;
                    default:
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setCurrentItem(0);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.message:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.contact:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.me:
                        viewPager.setCurrentItem(2);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });

    }
}